{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢欢真可爱！！！\n"
     ]
    }
   ],
   "source": [
    "## First python code in everyone's life\n",
    "print('欢欢真可爱！！！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data types and operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 3.0\n",
    "c = 1e2\n",
    "d = 'meander'\n",
    "e = \"wang\"\n",
    "f = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(int, float, float)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a), type(b), type(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(str, str)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(d), type(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a + b)  ## int 会向 float 妥协 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'meanderwang'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'meander@_@wang'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"@_@\".join([d, e])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Meander Wang'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\" \".join([d.capitalize(), e.capitalize()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Meander Wang'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = \" \".join([d.capitalize(), e.capitalize()])\n",
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "888465"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = 'IMG888465.jpg'\n",
    "int(name.lstrip('IMG').rstrip('.jpg'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ander Wang'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.lstrip('Me')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\" \".join([d.capitalize(), e.capitalize()]).rstrip('Wang')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "okk\n",
      "okk\n"
     ]
    }
   ],
   "source": [
    "if f:\n",
    "    print('okk')\n",
    "    \n",
    "if f is True:\n",
    "    print('okk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1\n",
    "b = 1\n",
    "a == b, a is b\n",
    "## Don't use `is`!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1\n",
    "b = [1, 2, 3]\n",
    "a in b, 5 in b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "阅读之星在我们班！\n"
     ]
    }
   ],
   "source": [
    "student_list = ['Xiaoming', 'Xiaohong', 'Xiaolan', 'Xiaolv']\n",
    "star_reading = 'Xiaohong'\n",
    "\n",
    "if star_reading in student_list:\n",
    "    print('阅读之星在我们班！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Control Flow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Huanhuan is cute!\n"
     ]
    }
   ],
   "source": [
    "if d == 'Meander':\n",
    "    print('Meander is cute!')\n",
    "elif e == 'Wang':\n",
    "    print('Wang is cute!')\n",
    "else:\n",
    "    print('Huanhuan is cute!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Meander is cute!\n"
     ]
    }
   ],
   "source": [
    "d = 'Meander'\n",
    "if d is 'Meander':\n",
    "    print('Meander is cute!')\n",
    "elif e is 'Wang':\n",
    "    print('Wang is cute!')\n",
    "elif f is not True:\n",
    "    print('Nothing!')\n",
    "else:\n",
    "    print('Huanhuan is cute!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# Python counts from ZERO!\n",
    "for i in range(0, 5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range(5, 9):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "for element in [1, 0, 2, 4]:\n",
    "    if element == 0:\n",
    "        continue\n",
    "    print(1.0 / element)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['huan']"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = []\n",
    "a.append('huan')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.4]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = []\n",
    "a.append(1.4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4], None)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2]\n",
    "b = [3, 4]\n",
    "a + b, a.append(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 'e', 'f']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2]\n",
    "b = ['e', 'f']\n",
    "a + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "def runnian(year, verbose=False):\n",
    "    year = int(year)\n",
    "    if year % 4 != 0:\n",
    "        if verbose:\n",
    "            print('This year is not RunNian!')\n",
    "        return False\n",
    "    \n",
    "    elif year % 400 != 0:\n",
    "        if verbose:\n",
    "            print('This year is not RunNian!')\n",
    "        return False\n",
    "    \n",
    "    else:\n",
    "        if verbose:\n",
    "            print('This year IS RunNian!')\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This year IS RunNian!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = runnian(2000, verbose=True)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "def area(radius):\n",
    "    import numpy as np\n",
    "    return np.pi * radius**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = area(1)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[34mFigures\u001b[m\u001b[m/            \u001b[1m\u001b[34mHW\u001b[m\u001b[m/                 Lec1-basic.md       basic-python.ipynb\r\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/jiaxuanli/Research/pynguin/basic\n"
     ]
    }
   ],
   "source": [
    "cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 8\r\n",
      "drwxr-xr-x  36 jiaxuanli  staff   1.1K Jan 20 11:56 \u001b[1m\u001b[34mmaterials\u001b[m\u001b[m/\r\n",
      "-rw-r--r--@  1 jiaxuanli  staff   1.8K Jan 20 11:55 HW-basic.md\r\n",
      "drwxr-xr-x   3 jiaxuanli  staff    96B Jan 20 11:54 \u001b[1m\u001b[34mcatalog\u001b[m\u001b[m/\r\n",
      "drwxr-xr-x   6 jiaxuanli  staff   192B Jan 20 11:52 \u001b[1m\u001b[34mcatalog_backup\u001b[m\u001b[m/\r\n"
     ]
    }
   ],
   "source": [
    "ls -lht"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Use your TAB when you don't know what to do!\n",
    "import numpy as np\n",
    "#np.#press TAB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 不懂就问\n",
    "np?\n",
    "np.linspace() # shift + tab\n",
    "np.linspace?\n",
    "np.linspace??\n",
    "np.*space?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Input & Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('testfile.txt', 'w')\n",
    "f.write(d)\n",
    "f.write('\\n')\n",
    "f.write(e)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat testfile.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('testfile.txt', 'r')\n",
    "text = f.read()\n",
    "f.close()\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text.split()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read-only: `r`\n",
    "\n",
    "Write-only: `w`.  Note: Create a new file or overwrite existing file.\n",
    "\n",
    "Append a file: `a`\n",
    "\n",
    "Read and Write: `r+`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conventions!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 注意标点符号，千万别打成中文的。因此这也是为什么要用英文写注释的原因。\n",
    "- 一定注意空格！！！\n",
    "- 一行不能写太长，80个字符就要换行（原则上），换行用`\\`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A bit of Numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  6.36826187,  -3.50781984,   9.69945843,   9.20783581,\n",
       "         0.0180619 ,  -3.99040463,   2.27547787,  -8.24958701,\n",
       "         7.02106596,   3.61228425,   2.11305407,   4.47949885,\n",
       "         4.58177588,  -3.42341741,   0.60393294,   4.21184635,\n",
       "         2.70627856,   1.19495244,   6.70618377,  -8.81653307,\n",
       "         3.57438565,   9.48240933,  -5.82693183,   1.18506737,\n",
       "         2.68074331,  -1.68549684,   1.4467738 ,   3.9643596 ,\n",
       "         2.88749247,   9.84411827,   4.55216925,  -0.93577358,\n",
       "         3.72738501,   6.46971829,   0.77917265,   3.37739346,\n",
       "         1.02242819,   2.02588182,  -3.18010652,  -0.42929157,\n",
       "         1.02756921,  -7.82580715,  -8.58690639,  -0.55174093,\n",
       "        -5.13835163,   8.41342772,  -1.98926702,  -1.22227705,\n",
       "        -2.21720897,  -3.92892297,   2.35457412,   2.78304648,\n",
       "        -4.70790408,   5.20979966,   7.14571319,  -4.67941148,\n",
       "         0.49415283,   8.94405709,  10.07193442,   4.65798428,\n",
       "         7.43355023,   4.8350322 ,  16.17606282,   3.55066078,\n",
       "         1.94599959,  -1.90924258,  -4.84330327,  -2.87596808,\n",
       "        -3.97195492,   2.95679251,   2.02195029,   7.46743537,\n",
       "        -0.135408  ,  -2.30142837,  -1.55438381,   9.06273696,\n",
       "       -10.36137425,  -5.89890988,  -0.68926481,  -1.62494612,\n",
       "         2.74806073,   3.99843676,  -5.16621357,  10.43331481,\n",
       "         0.63559975,   5.8602092 ,  -1.35269803,   6.74340353,\n",
       "         9.00677425,  -0.52098671,  -1.60989335,   6.14875527,\n",
       "         1.73414739,  -4.43126569,   2.79748178,  -3.26534233,\n",
       "         4.35885987,  -2.91319945,   1.91705658,   2.3482403 ])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "sample = np.random.normal(loc=1, scale=5, size=100)\n",
    "sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5.014305817812125, 1.5078334418904171, 1.9315280853505872)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(sample, ddof=1), np.mean(sample), np.median(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
